<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://unpkg.com/vue@3"></script>
  <script src="https://unpkg.com/vue-router@4"></script>
</head>
<body>
  <div id="app">
    <router-link to="/user/22">切换到用户组件</router-link>
    <router-view></router-view>
  </div>

  <script>
    const app = Vue.createApp({})

    const User = {
      template: '<div>User组件，路由参数{{$route.params}}</div>',
      setup() {
        const route = VueRouter.useRoute()
        Vue.onMounted(() => {
          console.log('我进来了')
        })
      }
    }

    const routes = [
      // { path: '/user/:id(\\d+)', component: User }
      // { path: '/user/:id*', component: User }
      { path: '/user/:id?', component: User }
    ]

    const router = VueRouter.createRouter({
      history: VueRouter.createWebHashHistory(),
      routes
    })

    app.use(router)

    app.mount('#app')
  </script>
</body>
</html>